package com.novunda.platform.common.dialects.shiro.processor;

import com.novunda.platform.common.dialects.shiro.ShiroFacade;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * @author linfeng
 * @since 2016/9/12
 */
public class NotAuthenticatedElementProcessor extends AbstractElementTagProcessor {

    private static final String TAG_NAME = "notAuthenticated";
    private static final int PRECEDENCE = 1200;

    public NotAuthenticatedElementProcessor(final String dialectPrefix) {
        super(TemplateMode.HTML, // This processor will apply only to HTML mode
                dialectPrefix,     // Prefix to be applied to name for matching
                TAG_NAME,          // Tag name: match specifically this tag
                true,              // Apply dialect prefix to tag name
                null,              // No attribute name: will match by tag name
                false,             // No prefix to be applied to attribute name
                PRECEDENCE);       // Precedence (inside dialect's own precedence)
    }


    @Override
    protected void doProcess(ITemplateContext context,
                             IProcessableElementTag tag,
                             IElementTagStructureHandler structureHandler) {

        if (!ShiroFacade.isNotAuthenticated()) {
            structureHandler.removeElement();
        }

    }
}
